package com.df.springmvc.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import com.df.springmvc.common.bean.MsgBean;
import com.df.springmvc.supper.GlobalVariable;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

/**
 * WorlkFlowUtil
 * @author David
 */
public class WFUtil{
	/**
	 * 获取 request中的参数
	 * @param request
	 * @param key
	 * @return
	 */
	private static String getParam(HttpServletRequest request,String key){
		String value="";
		String[] tmp=request.getParameterValues(key);
		if(tmp == null){
			return "";
		}else if(tmp.length == 1){
			value=tmp[0];
		}else{
			for(String t:tmp){
				value+="," + t;
			}
		}
		if(value.startsWith(",")){
			value=value.replaceFirst(",","");
		}
		value=CheckSqlInjectUtil.replace(value);
		System.out.println(key + ":" + value);
		return value;
	}
	/**
	 * 创建一个新流程,
	 * 先通过表单数据生成一条记录后,将数据的GUID传入doNew这个方法,生成一个与流程进行绑定的GUID
	 * @param proc_def_key
	 * @param map
	 * @return
	 */
	public static MsgBean doNew(String proc_def_key,String guid,String title,HashMap<String,Object> map){
		// TODO Auto-generated method stub
		String url="page/workflow/api/API/doNew";
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("proc_def_key",proc_def_key);
		tmp.put("act_form_guid",guid);
		tmp.put("title",title);
		tmp.put("map",map);
		Object rs=HTTPClientUtil.post(GlobalVariable.basePath + url,new Gson().toJson(tmp));
		MsgBean msgBean=new Gson().fromJson("" + rs,MsgBean.class);
		return msgBean;
	}
	/**
	 * 流程加签
	 * @param taskId
	 * @param map
	 * @return
	 */
	public static MsgBean doChain(String taskId,String userId,HashMap<String,Object> map){
		// TODO Auto-generated method stub
		String url="page/workflow/api/API/doChain";
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("act_form_taskId",taskId);
		tmp.put("userId",userId);
		tmp.put("map",map);
		Object rs=HTTPClientUtil.post(GlobalVariable.basePath + url,new Gson().toJson(tmp));
		MsgBean msgBean=new Gson().fromJson("" + rs,MsgBean.class);
		return msgBean;
	}
	/**
	 * 任务签收
	 * @param taskId
	 * @param map
	 * @return
	 */
	public static MsgBean doClaim(String taskId,String userId,HashMap<String,Object> map){
		// TODO Auto-generated method stub
		String url="page/workflow/api/API/doClaim";
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("act_form_taskId",taskId);
		tmp.put("map",map);
		Object rs=HTTPClientUtil.post(GlobalVariable.basePath + url,new Gson().toJson(tmp));
		MsgBean msgBean=new Gson().fromJson("" + rs,MsgBean.class);
		return msgBean;
	}
	/**
	 * 完成任务节点
	 * @param taskId
	 * @param userId
	 * @param map
	 * @return
	 */
	public static MsgBean doComplete(String taskId,String userId,HashMap<String,Object> map){
		// TODO Auto-generated method stub
		String url="page/workflow/api/API/doComplete";
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("act_form_taskId",taskId);
		tmp.put("userId",userId);
		tmp.put("map",map);
		Object rs=HTTPClientUtil.post(GlobalVariable.basePath + url,new Gson().toJson(tmp));
		MsgBean msgBean=new Gson().fromJson("" + rs,MsgBean.class);
		return msgBean;
	}
	/**
	 * 转派给其它人
	 * @param taskId
	 * @param userId
	 * @param map
	 * @return
	 */
	public static MsgBean doReAssignee(String taskId,String userId,HashMap<String,Object> map){
		// TODO Auto-generated method stub
		String url="page/workflow/api/API/doReAssignee";
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("act_form_taskId",taskId);
		tmp.put("userId",userId);
		tmp.put("map",map);
		Object rs=HTTPClientUtil.post(GlobalVariable.basePath + url,new Gson().toJson(tmp));
		MsgBean msgBean=new Gson().fromJson("" + rs,MsgBean.class);
		return msgBean;
	}
	/**
	 * 通过表单GUID查询当前所有任务详细
	 * @param guid
	 * @return ArrayList<HashMap<String,Object>>
	 */
	public static ArrayList<HashMap<String,Object>> queryTaskIdByGuid(String guid){
		String url="page/workflow/api/API/queryTaskIdByGuid";
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("act_form_guid",guid);
		Object rs=HTTPClientUtil.post(GlobalVariable.basePath + url,new Gson().toJson(tmp));
		MsgBean msgBean=new Gson().fromJson("" + rs,MsgBean.class);
		Object content=msgBean.getContent();
		if(content instanceof ArrayList){
			ArrayList<LinkedTreeMap<String,Object>> mapList=(ArrayList<LinkedTreeMap<String,Object>>)content;
			ArrayList<HashMap<String,Object>> mapList2=new ArrayList<HashMap<String,Object>>();
			for(LinkedTreeMap<String,Object> linkedTreeMap:mapList){
				mapList2.add(new HashMap<String,Object>(linkedTreeMap));
			}
			return mapList2;
		}else{
			return new ArrayList<HashMap<String,Object>>();
		}
	}
	/**
	 * 通过TaskId查询任务详细
	 * @param taskId
	 * @return HashMap<String,Object>
	 */
	public static HashMap<String,Object> queryTaskByTaskId(String taskId){
		String url="page/workflow/api/API/queryTaskByTaskId";
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("act_form_taskId",taskId);
		Object rs=HTTPClientUtil.post(GlobalVariable.basePath + url,new Gson().toJson(tmp));
		MsgBean msgBean=new Gson().fromJson("" + rs,MsgBean.class);
		Object content=msgBean.getContent();
		if(content instanceof LinkedTreeMap){
			return new HashMap<String,Object>((LinkedTreeMap)content);
		}else{
			return new HashMap<String,Object>();
		}
	}
	/**
	 * 自动获取请求中,流程相关的信息,如果有流程,这句必须调用.
	 * @param request
	 * @return act_form_guid,act_form_taskId
	 */
	public static void load(HttpServletRequest request){
		// TODO Auto-generated method stub
		String act_form_guid="";
		if(true){
			act_form_guid=getParam(request,"act_form_guid");
			if("".equals(act_form_guid)){
				act_form_guid=getParam(request,"guid");
			}
			if("".equals(act_form_guid)){
				act_form_guid=getParam(request,"map.guid");
			}
			request.setAttribute("act_form_guid",act_form_guid);
		}
		String act_form_taskId="";
		if(true){
			act_form_taskId=getParam(request,"act_form_taskId");
			request.setAttribute("act_form_taskId",act_form_taskId);
		}
		if(!"".equalsIgnoreCase(act_form_taskId)){
			request.setAttribute("act",queryTaskByTaskId(act_form_taskId));
		}
	}
}
